<template>
  <div class="cartcontrol">
    <transition name="move">
      <div
        class="cart-decrease"
        v-show="food.count > 0"
        @click.prevent.stop="decreaseCart"
      >
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="cart-count" v-show="food.count > 0">{{food.count}}</div>
    <div class="cart-add icon-add_circle" @click.prevent.stop="addCart"></div>
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  name: 'cartcontrol',
  props: {
    food: {
      type: Object
    }
  },
  methods: {
    addCart (e) {
      if (!this.food.count) {
        Vue.set(this.food, 'count', 1)
      } else {
        this.food.count++
      }
      // 向父组件派发事件，并传递当前dom元素
      this.$emit('add', e.target)
    },
    decreaseCart () {
      if (this.food.count) {
        this.food.count--
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  .cartcontrol
    font-size 0
    .cart-decrease
      display inline-block
      padding .12rem
      transition all .4s linear
      transform translate3d(0,0,0)
      opacity 1
      .inner
        display inline-block
        text-align center
        font-size .48rem
        line-height .48rem
        color rgb(0,160,220)
        transition all 0.4s linear
        transform rotate(0deg)
      &.move-enter, &.move-leave-active
        transition all .4s linear
        opacity 0
        transform translate3d(.48rem,0,0)
        .inner
          transform rorate(180deg)
    .cart-count
      vertical-align top
      display inline-block
      width .24rem
      padding-top .12rem
      line-height .48rem
      text-align center
      font-size .2rem
      color rgb(147,153,159)
    .cart-add
      display inline-block
      padding .12rem
      line-height .48rem
      font-size .48rem
      color rgb(0,160,220)
</style>
